#include "stm32f10x_conf.h"


static volatile uint32_t g_sys_time = 0;  // us


void SysTick_Init(void) {
    // 72MHz / 1000 000 = 1 us
    SysTick_Config(SystemCoreClock/1000000);
}


/** Interrupt handler */
void SysTick_Handler(void) {
    g_sys_time++;
}


/** @brief Get current time, in microseconds. */
uint32_t get_time_us(void) {
    return g_sys_time;
}


/** @brief Delay in microseconds. */
void delay_us(uint32_t t) {
    static uint32_t _last_t;
    uint32_t _curr_t = get_time_us();
    
    _last_t = _curr_t;
    while (_curr_t - _last_t < t) {
        _curr_t = get_time_us();
    }
}
